Imports System.Data.OracleClient

Public Class frmGnrVar
  Private mStrSearch As String = String.Empty
  Dim clsobjGenVar As New clsGnrVar

  Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    ' Add any initialization after the InitializeComponent() call.

  End Sub

  Private Sub frmGnrtScript_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim mDrData As OracleDataReader
    Dim lStrCategory As String = String.Empty

    mDrData = clsobjGenVar.mfnGetCategory()
    While mDrData.Read

      lStrCategory = mDrData("TDCM_DATA_CTGRY")
      cboCategory.Items.Add(lStrCategory)
    End While

    LoadVariable()
  End Sub

  Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click

    Dim lStrVarName As String = String.Empty
    Dim lStrVarNameTemp As String = String.Empty
    Dim lStrCode As String = String.Empty
    Dim lStrCategory As String = String.Empty
    Dim lIntMaxSerial As Integer = 0

    If Trim(txtVarName.Text) = String.Empty Then
      MessageBox.Show("Varible name cannot be blank", gCONST_MSG_HEADER)
      cmdSelectName.Focus()
      Exit Sub
    End If
    If cboCategory.Text = String.Empty Then
      MessageBox.Show("Category cannot be blank", gCONST_MSG_HEADER)
      cboCategory.Focus()
      Exit Sub
    End If

    lStrCategory = cboCategory.Text
    lStrVarName = Trim(txtVarName.Text)
    lStrVarName = lStrVarName + "_" + lStrCategory
    lStrVarNameTemp = lStrVarName.Replace("_", " ")
    lStrVarNameTemp = StrConv(lStrVarNameTemp, VbStrConv.ProperCase)
    lStrVarNameTemp = lStrVarNameTemp.Replace(" ", String.Empty)

    lIntMaxSerial = clsobjGenVar.mfnGetMaxSerial + 1

    lStrCode = "INSERT INTO TRDX_APLCTN_VRBLS(TAV_SERIAL_NMBR,TAV_VRBL_DSCRPTN,TAV_CLMN_DSCRPTN,TAV_DATA_CTGRY) VALUES("
    lStrCode = lStrCode + lIntMaxSerial.ToString + ",'" + lStrVarNameTemp + "','" + lStrVarName + "','" + lStrCategory + "')"
    lStrCode = lStrCode + String.Empty   ' for debug

    If clsobjGenVar.mfnAddNewAppVar(lStrCode) Then
      MessageBox.Show("Add a new applcation variable successfully", gCONST_MSG_HEADER)
    Else
      MessageBox.Show("Fail to add new applcation variable", gCONST_MSG_HEADER)
    End If

    LoadVariable()
  End Sub

  Private Sub cmdSelectName_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSelectName.Click
    Dim lIntVarNameLength As Integer = 0
    Dim lStrVarName As String = String.Empty

    Dim lObjFrmTableNameSelection As New frmTableNameAbbreviation
    lObjFrmTableNameSelection.Text = "Select Variable Name"

    lObjFrmTableNameSelection.ShowDialog()

    lIntVarNameLength = lObjFrmTableNameSelection.mStrColumnName.Length
    'If lIntVarNameLength <= 0 Then
    '  MessageBox.Show("Please select variables name", gCONST_MSG_HEADER)
    '  Exit Sub
    'End If

    If lIntVarNameLength <= 0 Then
      txtVarName.Text = String.Empty
    Else
      txtVarName.Text = UCase(lObjFrmTableNameSelection.mStrColumnName.Substring(0, lIntVarNameLength - 1))
    End If

  End Sub

  Private Sub dgvVarLst_GotFocus(ByVal sender As Object _
                               , ByVal e As System.EventArgs) Handles dgvVarLst.GotFocus

    timTimer.Enabled = True

  End Sub

  Private Sub dgvVarLst_KeyDown(ByVal sender As Object _
                              , ByVal e As System.Windows.Forms.KeyEventArgs _
                              ) Handles dgvVarLst.KeyDown


    Dim lIntKeyValue As Integer = e.KeyValue

    Dim lChrKey As String = Chr(lIntKeyValue).ToString

    If (Char.IsLetter(lChrKey)) Then
      mStrSearch = mStrSearch & lChrKey
      For Each lObjRow As DataGridViewRow In Me.dgvVarLst.Rows
        Dim lObjCellValue As Object = lObjRow.Cells("clmVaribleName").Value
        If (lObjCellValue.ToString().ToLower().StartsWith(LCase(mStrSearch))) Then
          lObjRow.Selected = True
          Me.dgvVarLst.FirstDisplayedScrollingRowIndex = lObjRow.Index
          Exit For
        End If
      Next
    End If
  End Sub

  Private Sub timTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles timTimer.Tick

    mStrSearch = String.Empty

  End Sub

  Private Sub LoadVariable()
    Dim lCmdData As OracleCommand
    Dim lDrData As OracleDataReader
    Dim lStrQuery As String = String.Empty
    Dim lIntRowIndex As Integer

    mPrRefreshConn()
    dgvVarLst.Rows.Clear()
    lStrQuery = "SELECT * FROM TRDX_APLCTN_VRBLS T ORDER BY T.TAV_VRBL_DSCRPTN, T.TAV_SERIAL_NMBR"
    lCmdData = New OracleCommand(lStrQuery, con)
    lDrData = lCmdData.ExecuteReader
    If lDrData.HasRows Then
      While lDrData.Read
        lIntRowIndex = dgvVarLst.Rows.Add()
        With dgvVarLst.Rows(lIntRowIndex)
          .Cells("clmVaribleName").Value = lDrData.Item("TAV_VRBL_DSCRPTN")
          .Cells("clmVerClmDesc").Value = lDrData.Item("TAV_CLMN_DSCRPTN")
          .Cells("clmCategory").Value = lDrData.Item("TAV_DATA_CTGRY")
          .Cells("clmSrno").Value = lDrData.Item("TAV_SERIAL_NMBR")
        End With
      End While
    End If
  End Sub

End Class
